package com.helloandroid.dialogs

import android.app.Activity
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.helloandroid.AppUtil
import com.helloandroid.MyApplication
import com.helloandroid.R
import com.helloandroid.ads.NativeAdUtil
import com.helloandroid.ads.VideoAdUtil
import com.helloandroid.base.BaseDialog
import com.helloandroid.tools.MyLog
import com.helloandroid.tools.NetWorkUtil


data class ZpParams(val imgId:Int,val count:Int)

class ZpResultDialog(private val act:Activity, val params: ZpParams) : BaseDialog(act) {

    // 奖励的东西, 金币或者碎片
    private lateinit var btnDouble:Button
    lateinit var tvCount:TextView
    lateinit var ivIcon:ImageView

    var isDoubleGet = false  // 是否双倍

    override fun getContentViewResId(): Int = R.layout.dialog_zp_result

    override fun initView() {
        MyLog.elog("ZpResultDialog::initView called!")
        isDoubleGet = (params.count != 1)
        btnDouble = findViewById(R.id.btnDouble)
        tvCount  = findViewById(R.id.tvCount)
        ivIcon = findViewById(R.id.ivIcon)

        findBtn(R.id.ivBtnClose).setOnClickListener { cancel() }

        tvCount.text = "X${params.count}"
        ivIcon.setImageResource(params.imgId)

        if (isDoubleGet){
            btnDouble.text = "双倍领取"
        }else{
            btnDouble.text = "关闭"
        }
        btnDouble.setOnClickListener {
            if (isDoubleGet){
                playVideo()
            }else{
                cancel()
            }
        }

        NativeAdUtil.showNativeAd(act,findViewById(R.id.nativeView))
    }


    override fun initData() {
    }
    private fun playVideo(){
        if (!NetWorkUtil.networkConnected()){
            AppUtil.toast("请打开网络!")
            return
        }
        MyLog.elog("看视频,领取奖励......")

        VideoAdUtil.playVideoAd(ctx as Activity){success->
            if (success){
                AppUtil.toast("恭喜领取${params.count}金币!")
                val appModel = AppUtil.appViewModel()
                appModel.doubleAward(params.count)
                cancel()
            }else{
                AppUtil.toast("视频加载失败,请稍后再试!")
            }
        }


    }
}